import { reqGetCode, reqUserRegister, reqUserLogin, reqUserInfo, reqLogout } from "@/api";
import { setToken, getToken, removeToken } from '@/utils/token'
const state = {
  code: '',
  token: getToken(),
  userInfo: {}
}
const mutations = {
  GETCODE (state, code) {
    state.code = code
  },
  USERLOGIN (state, token) {
    state.token = token
  },
  GETUSERINFO (state, userInfo) {
    state.userInfo = userInfo
  },
  // 本地所有数据清空
  CLEAR (state) {
    state.token = '',
      state.userInfo = {},
      removeToken()
  }

}
const actions = {
  // 获取验证码
  async getCode ({ commit }, phone) {
    // 验证码服务器太贵了，用自己产生，自己获取
    let result = await reqGetCode(phone)
    if (result.code == 200) {
      commit("GETCODE", result.data)
      return 'OK'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  // 用户注册
  async userRegister ({ commit }, user) {
    let result = await reqUserRegister(user)
    if (result.code == 200) {
      return 'OK'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  // 登录
  async userLogin ({ commit }, data) {
    let result = await reqUserLogin(data)
    if (result.code == 200) {
      commit('USERLOGIN', result.data.token)
      // 持久化存储token
      setToken(result.data.token)
      console.log(result.data)
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  // 获取用户信息
  async getUserInfo ({ commit }) {
    let result = await reqUserInfo()
    if (result.code == 200) {
      commit('GETUSERINFO', result.data)
      console.log(result.data)

      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
  // 退出登录
  async userLogout ({ commit }) {
    // 给服务器请求退出 
    let result = await reqLogout()
    if (result.code == 200) {
      // 清除本地数据
      commit("CLEAR")
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  }
}
const getters = {}
export default {
  getters,
  actions,
  mutations,
  state
}